package com.block.datapush.form.service.impl;

import cn.hutool.core.net.multipart.UploadFile;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.block.common.exception.NotifyExceptionService;
import com.block.common.util.BigDecimalUtil;
import com.block.datapush.form.service.IFormInfoProfitService;
import com.block.datapush.form.enums.PushActionTypeEnum;
import com.block.db.entity.FormInfo;
import com.block.db.entity.FormInfoProfit;
import com.block.db.mapper.FormInfoProfitMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2022-12-12
 */
@Service
public class FormInfoProfitServiceImpl extends ServiceImpl<FormInfoProfitMapper, FormInfoProfit> implements IFormInfoProfitService {

    @Autowired
    NotifyExceptionService notifyExceptionService;


    @Override
    public boolean initProfitInfo(FormInfo formInfo) {

        String nowDate = LocalDate.now().toString();

        boolean result = false;
        FormInfoProfit formInfoProfit = this.baseMapper.getByInfoIdAndDate(formInfo.getId(), nowDate);
        if (ObjectUtil.isEmpty(formInfoProfit)){
            // 新增收益信息
            formInfoProfit = new FormInfoProfit();
            formInfoProfit.setInfoId(formInfo.getId());
            formInfoProfit.setInfoName(formInfo.getChannelName());
            formInfoProfit.setDateInfo(nowDate);
            formInfoProfit.setTotalNumber(1);
            result = this.save(formInfoProfit);
        }else{
            // 更新收益信息
            FormInfoProfit updateProfit = new FormInfoProfit();
            updateProfit.setId(formInfoProfit.getId());
            updateProfit.setTotalNumber(formInfoProfit.getTotalNumber() + 1);
            result = this.updateById(updateProfit);
        }
        return result;
    }

    @Override
    public boolean updateProfitInfo(FormInfoProfit formInfoProfit) {
        formInfoProfit.setUpdateTime(LocalDateTime.now());
        return this.baseMapper.updateProfitInfo(formInfoProfit) > 0;
    }

}
